﻿using LD.Controls.ControlHelper;
using LD.Host;
using System;
using System.Windows.Forms;

namespace LD
{
    public partial class FrmMachineParame : FrmBase
    {
        private ReDrawUI ReDrawUI = new ReDrawUI();
        //ScrewAdr singleScrewAdr;//单个
        public FrmMachineParame()
        {
            InitializeComponent();
            UserHelper.Instance.UserChangeEvent += UserChangeEvent;
            this.Shown += FrmMachineParame_Shown;
            CreateUI();
            FormScaler.ScaleForm(this, true);//自动缩放
        }

        private void FrmMachineParame_Shown(object sender, EventArgs e)
        {
            Init();
        }

        private void UserChangeEvent(UserModel user)
        {
            bool IsUnAdmin = false;//管理员
            if (user != null && user.UserType.Equals(UserType.管理员))
            {

                IsUnAdmin = false;
                btn_save.Show();
            }
            else
            {

                IsUnAdmin = true;
                btn_save.Hide();
            }

            tb_MaxAngle.ReadOnly = IsUnAdmin;
            tb_MaxTorque.ReadOnly = IsUnAdmin;
            tb_MinAngle.ReadOnly = IsUnAdmin;
            tb_MinTorque.ReadOnly = IsUnAdmin;
            tb_TargetTorque.ReadOnly = IsUnAdmin;
            this.txt_location.ReadOnly = IsUnAdmin;
        }

        private void FrmMachineParame_Load(object sender, EventArgs e)
        {
            this.Resize += new EventHandler(FrmMachineParame_Resize);
            ReDrawUI.ControlInitializeSize(this);
            btn_save.Hide();
        }
        private void FrmMachineParame_Resize(object sender, EventArgs e)
        {
            ReDrawUI.ControlAutoSize(this);
        }
        private void CreateUI()
        {
            tb_MaxAngle.ReadOnly = true;
            tb_MaxTorque.ReadOnly = true;
            tb_MinAngle.ReadOnly = true;
            tb_MinTorque.ReadOnly = true;
            tb_TargetTorque.ReadOnly = true;
            txt_location.ReadOnly = true;
        }
        private void Init()
        {
            tb_MaxAngle.Text = string.Empty;//最大角度
            tb_MaxTorque.Text = string.Empty;//最大扭矩
            tb_MinAngle.Text = string.Empty;//最小角度
            tb_MinTorque.Text = string.Empty;//最小扭矩
            tb_TargetTorque.Text = string.Empty;//目标扭矩

            if (PlcCommManger.Instance.PLCScrewAdrs == null)
                return;

            foreach (var sc in PlcCommManger.Instance.PLCScrewAdrs)//添加
            {
                cb_selectScrew.Items.Add(sc.Name);
            }
            cb_selectScrew.SelectedIndex = 0;
        }
        private void cb_selectScrew_SelectedIndexChanged(object sender, EventArgs e)
        {
            groupBox1.Text = cb_selectScrew.Text;//groupbox改变

            var dicScrewInfo = PartNoHelper.Instance.CurrentPartNo.Modules[cb_selectScrew.SelectedIndex].Parame;

            if (dicScrewInfo == null || dicScrewInfo.Count == 0)
                return;
            tb_MaxAngle.Text = dicScrewInfo["MaxAngle"];
            tb_MaxTorque.Text = dicScrewInfo["MaxTorque"];
            tb_MinAngle.Text = dicScrewInfo["MinAngle"];
            tb_MinTorque.Text = dicScrewInfo["MinTorque"];
            tb_TargetTorque.Text = dicScrewInfo["TargetTorque"];
            txt_location.Text = dicScrewInfo["Location"];

            tb_MaxAngle.Tag = dicScrewInfo["MaxAngle"];
            tb_MaxTorque.Tag = dicScrewInfo["MaxTorque"];
            tb_MinAngle.Tag = dicScrewInfo["MinAngle"];
            tb_MinTorque.Tag = dicScrewInfo["MinTorque"];
            tb_TargetTorque.Tag = dicScrewInfo["TargetTorque"];
            txt_location.Tag = dicScrewInfo["Location"];

        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_save_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cb_selectScrew.Text))//未选择
                return;

            if (tb_TargetTorque.Tag.ToString() != tb_TargetTorque.Text)
            {
                double.TryParse(tb_TargetTorque.Text, out var val);

                if (val != 0)
                {
                    PartNoHelper.Instance.CurrentPartNo.Modules[cb_selectScrew.SelectedIndex].Parame["TargetTorque"] = val + "";
                    Log4.ParamChange($"修改电批参数【{cb_selectScrew.Text}】,参数【目标扭矩】,原值【{tb_TargetTorque.Tag}】修改为【{this.tb_TargetTorque.Text}】");
                    tb_TargetTorque.Tag = tb_TargetTorque.Text;
                }
                else
                    MessageBox.Show("操作失败，目标扭矩 不可为空或非法字符");
            }

            if (tb_MaxTorque.Tag.ToString() != tb_MaxTorque.Text)
            {
                double.TryParse(tb_MaxTorque.Text, out var val);

                if (val != 0)
                {
                    PartNoHelper.Instance.CurrentPartNo.Modules[cb_selectScrew.SelectedIndex].Parame["MaxTorque"] = val + "";
                    Log4.ParamChange($"修改电批参数【{cb_selectScrew.Text}】,参数【最大扭矩】,原值【{tb_MaxTorque.Tag}】修改为【{this.tb_MaxTorque.Text}】");
                    tb_MaxTorque.Tag = tb_MaxTorque.Text;
                }
                else
                    MessageBox.Show("操作失败，最大扭矩 不可为空或非法字符");
            }

            if (tb_MinTorque.Tag.ToString() != tb_MinTorque.Text)
            {
                double.TryParse(tb_MinTorque.Text, out var val);

                if (val != 0)
                {
                    PartNoHelper.Instance.CurrentPartNo.Modules[cb_selectScrew.SelectedIndex].Parame["MinTorque"] = val + "";
                    Log4.ParamChange($"修改电批参数【{cb_selectScrew.Text}】,参数【最小扭矩】,原值【{tb_MinTorque.Tag}】修改为【{this.tb_MinTorque.Text}】");
                    tb_MinTorque.Tag = tb_MinTorque.Text;
                }
                else
                    MessageBox.Show("操作失败，最大扭矩 不可为空或非法字符");
            }

            if (tb_MaxAngle.Tag.ToString() != tb_MaxAngle.Text)
            {
                double.TryParse(tb_MaxAngle.Text, out var val);
                if (val != 0)
                {
                    PartNoHelper.Instance.CurrentPartNo.Modules[cb_selectScrew.SelectedIndex].Parame["MaxAngle"] = val + "";
                    Log4.ParamChange($"修改电批参数【{cb_selectScrew.Text}】,参数【最大角度】,原值【{tb_MaxAngle.Tag}】修改为【{this.tb_MaxAngle.Text}】");
                    tb_MaxAngle.Tag = tb_MaxAngle.Text;
                }

                else
                    MessageBox.Show("操作失败，最大扭矩 不可为空或非法字符");
            }

            if (tb_MinAngle.Tag.ToString() != tb_MinAngle.Text)
            {
                double.TryParse(tb_MinAngle.Text, out var val);

                if (val != 0)
                {
                    PartNoHelper.Instance.CurrentPartNo.Modules[cb_selectScrew.SelectedIndex].Parame["MinAngle"] = val + "";
                    Log4.ParamChange($"修改电批参数【{cb_selectScrew.Text}】,参数【最小角度】,原值【{tb_MinAngle.Tag}】修改为【{this.tb_MinAngle.Text}】");
                    tb_MinAngle.Tag = tb_MinAngle.Text;
                }

                else
                    MessageBox.Show("操作失败，最大扭矩 不可为空或非法字符");
            }

            if (txt_location.Tag.ToString() != txt_location.Text)
            {
                PartNoHelper.Instance.CurrentPartNo.Modules[cb_selectScrew.SelectedIndex].Parame["Location"] = txt_location.Text;
                Log4.ParamChange($"修改电批参数【{cb_selectScrew.Text}】,参数【螺丝序号】,原值【{txt_location.Tag}】修改为【{this.txt_location.Text}】");
                txt_location.Tag = txt_location.Text;
            }
            PartNoHelper.Instance.CurrentPartNo.Save();
            MessageBox.Show($"保存完成");
        }
    }
}
